package com.nikanbujian.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nikanbujian.reggie.common.R;
import com.nikanbujian.reggie.config.RedisConfig;
import com.nikanbujian.reggie.entity.User;
import com.nikanbujian.reggie.service.IUserService;
import com.nikanbujian.reggie.utils.SMSUtils;
import com.nikanbujian.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-08-22
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IUserService iUserService;
//    @PostMapping("/login")
//    public R<String> login(int phone){
//
//    }
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){

        String phone = user.getPhone();
        if (StringUtils.isNotEmpty(phone)){
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("code=   {}   ",code);
//            SMSUtils.sendMessage("外卖","",phone,code);
//            session.setAttribute(phone,code);
//            log.info("123456{}",session.getAttribute(phone).toString());
            //将生成的验证码放入redis
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);



            return R.success("发送成功");
        }
       return R.error("发送失败");
    }
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpSession session){

        log.info(map.toString());
//        session.getAttribute("")
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();
//        Object code1 = session.getAttribute(phone);


        //从redis拿出验证码
        Object code1 = redisTemplate.opsForValue().get(phone);

        if (code1!=null && code.equals(code1)){
            LambdaQueryWrapper<User> userLambdaQueryWrapper=new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.eq(User::getPhone,phone);
            User user = iUserService.getOne(userLambdaQueryWrapper);
            if (user==null){
                user=new User();
                user.setPhone(phone);
                user.setStatus(1);
                iUserService.save(user);
            }
            session.setAttribute("user",user.getId());

            redisTemplate.delete(phone);
            return R.success(user);
        }
        return R.error("登录失败");
    }

}
